import django_filters
from django_filters.rest_framework import DjangoFilterBackend, FilterSet

from mysqldb.models import AllinpayAuthViews

from django.conf import settings


class ListDjangoFilterBackend(DjangoFilterBackend):
    """自己定义list过滤器"""

    def get_schema_fields(self, view):
        if view.action not in ["list"]:
            return []
        return super().get_schema_fields(view)


class AllinpayAuthViewsFilterSet(FilterSet):
    """视图列表过滤器"""

    act = django_filters.CharFilter(field_name="act", help_text="请求方法")
    staff = django_filters.NumberFilter(
        field_name="staff", required=False, method="filter_staff", help_text="类型ID"
    )

    def has_args_list(self, idx):
        """查询数列"""
        y = []
        x = settings.PLACE_CHMOD_LIST
        for i in range(len(x)):
            if idx in x[i]:
                y.append(i)
        print(y)
        return y

    def filter_staff(self, qs, name, value):
        # lookup = "__".join([name, "lte"])
        lookup = "__".join([name, "in"])
        return qs.filter(**{lookup: self.has_args_list(value)})

    class Meta:
        model = AllinpayAuthViews
        fields = ["staff", "act"]
